﻿namespace WebCamera
{
    using System;
    using System.Text;

    internal class WindowEumerator
    {
        public string _title;
        public IntPtr win;

        public WindowEumerator(string title)
        {
            this._title = title;
        }

        public void Search()
        {
            OSSupport.EnumWindowProc eWP = new OSSupport.EnumWindowProc(this.WindowCallback);
            OSSupport.EnumWindows(eWP, 0);
        }

        private bool WindowCallback(IntPtr hWnd, int lParam)
        {
            StringBuilder lpString = new StringBuilder(0x100);
            try
            {
                OSSupport.GetWindowText(hWnd, lpString, 0x100);
                if (lpString.ToString().IndexOf(this._title) >= 0)
                {
                    this.win = hWnd;
                    return false;
                }
                //Log.Write(string.Concat(new object[] { "HWND:", hWnd, "TITLE:", lpString.ToString() }));
            }
            catch (Exception exception)
            {
                Log.Write(exception);
                throw new PCException();
            }
            return true;
        }
    }
}

